//
//  RSEnumPickerViewController.m
//  StaffTax
//
//  Created by 侯伟 on 2020/1/3.
//  Copyright © 2020 侯伟. All rights reserved.
//

#import "RSEnumPickerViewController.h"
#import <MyLayout/MyLayout.h>

@interface RSEnumPickerViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>{
    NSUInteger index;
}

@property (strong, nonatomic) UILabel *titleLb;

@end

@implementation RSEnumPickerViewController

- (void)loadView{
    [super loadView];
    
    MyLinearLayout* popView = [[MyLinearLayout alloc] initWithOrientation:(MyOrientation_Vert)];
    popView.backgroundColor = UIColor.whiteColor;
    popView.myHorzMargin = 0;
    popView.myBottom = 0;
    popView.myHeight = MyLayoutSize.wrap;
    [self.view addSubview:popView];
    self.popView = popView;
    popView.endLayoutBlock = ^{
        [self.popView setCornerRadius:(UIRectCornerTopLeft|UIRectCornerTopRight) size:CGSizeMake(PX(12), PX(12))];
        self.popView.layer.masksToBounds = YES;
    };
    

    MyLinearLayout* titleLayout = [[MyLinearLayout alloc] initWithOrientation:(MyOrientation_Horz)];
    titleLayout.padding = UIEdgeInsetsMake(0, 0, 0, 0);
    titleLayout.myHeight = PX(60.0);
    titleLayout.myMargin = 0;
    [popView addSubview:titleLayout];
    
    MyBorderline* line = [MyBorderline new];
    line.color = COLOR_GRAY_LINE;
    line.thick = 1.0;
    titleLayout.bottomBorderline = line;
    
    
    UIButton* cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:HEXCOLOR(0x727E8C) forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = FONT_FIT(14);
    [cancelBtn addTarget:self action:@selector(cancelSelect:) forControlEvents:UIControlEventTouchUpInside];
    cancelBtn.mySize = CGSizeMake(70, 32);
    cancelBtn.myCenterY = 0;
    cancelBtn.myLeft = 0;
    [titleLayout addSubview:cancelBtn];
    
    UILabel* titleLb = [UILabel lableWithFont:FONT_FIT(16) color:COLOR_BLACK_DARK text:NULL];
    titleLb.textAlignment = NSTextAlignmentCenter;
    titleLb.myHorzMargin = 15.0;
    titleLb.myCenterY = 0;
    titleLb.weight = 1.0;
    [titleLayout addSubview:titleLb];
    _titleLb = titleLb;
    _titleLb.text = self.titleStr;
    [_titleLb sizeToFit];
    
    UIButton* sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:COLOR_THEME forState:UIControlStateNormal];
    sureBtn.titleLabel.font = FONT_FIT(14);
    [sureBtn addTarget:self action:@selector(doneObject:) forControlEvents:UIControlEventTouchUpInside];
    sureBtn.mySize = CGSizeMake(70, 32);
    sureBtn.myCenterY = 0;
    sureBtn.myRight = 0;
    [titleLayout addSubview:sureBtn];
    
    UIPickerView* picker = [UIPickerView new];
    picker.dataSource = self;
    picker.delegate = self;
    picker.myMargin = 0;
    picker.myHeight = PX(275.0);
    [popView addSubview:picker];
    _pickerView = picker;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [self.dataArray enumerateObjectsUsingBlock:^(EnumObj*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if(self.value == obj.value){
            index = idx;
            *stop = YES;
        }
    }];
    if(index < self.dataArray.count){
        [self.pickerView selectRow:index inComponent:0 animated:YES];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - Navigation

- (void)doneObject:(UIButton *)sender {
    
    EnumObj* obj = self.dataArray[index];
    
    if(self.selectedBlock){
        self.selectedBlock(obj,self->index);
    }
    
    [self dismissActionSheetController:NULL];
}


- (void)cancelSelect:(UIButton *)sender {
    [self dismissActionSheetController:NULL];
    
    if (self.cancelBlockClick) {
        self.cancelBlockClick();
    }
}

#pragma -- UIPickerViewDataSource, UIPickerViewDelegate

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.dataArray.count;
}

- (UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *customLabel = (UILabel *)view;
    if (!customLabel) {
        customLabel = [[UILabel alloc] init];
        customLabel.textAlignment = NSTextAlignmentCenter;
        customLabel.textColor = COLOR_BLACK_DARK;
        [customLabel setFont:FONT_FIT(15)];
    }
    
    EnumObj* obj = self.dataArray[row];
    customLabel.text = obj.label;
    return customLabel;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return CGRectGetWidth(pickerView.frame);
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return PX(55);
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    index = row < 0 ? 0 : row;
}

@end
